package com.jiuzhou.guanwang.jzcp.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.google.gson.Gson;
import com.jiuzhou.guanwang.jzcp.R;
import com.jiuzhou.guanwang.jzcp.activity.MyWebActivity;
import com.jiuzhou.guanwang.jzcp.adapter.ScheduleAdapter;
import com.jiuzhou.guanwang.jzcp.base.BaseFragment;
import com.jiuzhou.guanwang.jzcp.bean.ScheduleBean;
import com.jiuzhou.guanwang.jzcp.bean.ScheduleResult;
import com.jiuzhou.guanwang.jzcp.netutil.NetRequestUtil;
import com.jiuzhou.guanwang.jzcp.utils.CommonUtils;
import com.jiuzhou.guanwang.jzcp.utils.TimeUtil;
import com.vise.log.ViseLog;

import org.xutils.common.Callback;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ScheduleFragment extends BaseFragment {
    private ScheduleAdapter mJclqAdapter;
    private ListView mJclqLv;
    private List<ScheduleBean> data;

    protected void initView(View view) {
        this.mJclqLv = (ListView) view.findViewById(R.id.lv_sc);
    }

    protected int bindLayout() {
        return R.layout.fragment_world_schedule;
    }

    protected void initData() {
        Map<String, String> map = new HashMap<>();
        String time = TimeUtil.getDateTimeFromMillisecond(System.currentTimeMillis());
        map.put("start", time);
        map.put("init", "1");
        Callback.Cancelable post = NetRequestUtil.getInstance().get("https://api.dongqiudi.com/data/tab/league/new/61", map, 101,
                new NetRequestUtil.NetResponseListener<String>() {
                    @Override
                    public void onSuccess(String response, int requestCode) {
                        ViseLog.d(response);
                        ScheduleResult result = (ScheduleResult) new Gson().fromJson(response, ScheduleResult.class);
                        if (result != null && !CommonUtils.isEmpty(result.getList())) {
                            if (mJclqAdapter == null) {
                                mJclqAdapter = new ScheduleAdapter(getActivity());
                                mJclqLv.setAdapter(mJclqAdapter);
                            }
                            data = result.getList();
                            mJclqAdapter.setData(result.getList());
                            mJclqLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                @Override
                                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                    Intent intent = new Intent(getActivity(), MyWebActivity.class);
                                    String url = " https://n.dongqiudi.com/pre_analysis/" + data.get(position).getMatch_id() + "?platform=iphone&version=600";

                                    intent.putExtra("teama", data.get(position).getTeam_A_name());
                                    intent.putExtra("teamb", data.get(position).getTeam_B_name());
                                    intent.putExtra("url", url);
                                    startActivity(intent);
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailed(String response, int requestCode) {
                        ViseLog.e("请求失败");
                        showToast("返回失败");
                    }

                    @Override
                    public void onError(Throwable e) {
                        showToast("返回解析异常");
                    }

                    @Override
                    public void onFinished() {
                    }
                });
    }
}
